Crate jaq_interpret

source ·
Expand description

JSON query language interpreter.

This crate allows you to execute jq-like filters.

The example below demonstrates how to use this crate. See the implementation in the jaq crate if you are interested in how to:

  • enable usage of the standard library,
  • load JSON files lazily,
  • handle errors etc.
use jaq_interpret::{Ctx, Error, FilterT, ParseCtx, RcIter, Val};
use serde_json::{json, Value};

let input = json!(["Hello", "world"]);
let filter = ".[]";

// start out only from core filters,
// which do not include filters in the standard library
// such as `map`, `select` etc.
let mut defs = ParseCtx::new(Vec::new());

// parse the filter
let (f, errs) = jaq_parse::parse(filter, jaq_parse::main());
assert_eq!(errs, Vec::new());

// compile the filter in the context of the given definitions
let f = defs.compile(f.unwrap());
assert!(defs.errs.is_empty());

let inputs = RcIter::new(core::iter::empty());

// iterator over the output values
let mut out = f.run((Ctx::new([], &inputs), Val::from(input)));

assert_eq!(out.next(), Some(Ok(Val::from(json!("Hello")))));;
assert_eq!(out.next(), Some(Ok(Val::from(json!("world")))));;
assert_eq!(out.next(), None);;

Re-exports§

Modules§

  • Runtime errors.
  • Functions on iterators over results.

Structs§

  • Arguments passed to a native filter.
  • Filter execution context.
  • Function from a value to a stream of value results.
  • A filter which is implemented using function pointers.
  • Compile parsed to executable filters.
  • A more flexible version of &mut impl Iterator.

Enums§

  • JSON value with sharing.

Traits§

  • Function from a value to a stream of value results.
  • Values that can be processed by the interpreter.

Type Aliases§